<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业模块api配置 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;

use think\Cache;
use think\Db;

class EnterpriseModuleApi {
    const CACHE_KEY = "core.EnterpriseModuleApi.";

    private static function _k($eid, $mid) {
        return self::CACHE_KEY . $eid . "." . $mid;
    }

    /**
     * 清理缓存
     *
     * @param $eid
     * @param $mid
     */
    private static function clearCache($eid, $mid) {
        Cache::rm(self::_k($eid, $mid));
    }

    /**
     * 更新api信息
     *
     * @param $eid
     * @param $mid
     * @param $api
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function update($eid, $mid, $api, $data) {
        $record = self::_q()->where([
            'enterprise_id' => $eid,
            'module_id' => $mid,
            'api' => $api
        ])->find();
        if (empty($record)) {
            $data = array_merge($data, [
                'enterprise_id' => $eid,
                'module_id' => $mid,
                'api' => $api
            ]);
            $ret = self::_q()->insertGetId($data);
        } else {
            $ret = self::_q()->where([
                'enterprise_id' => $eid,
                'module_id' => $mid,
                'api' => $api
            ])->update($data);
        }
        self::clearCache($eid, $mid);
        return $ret;
    }

    /**
     * 获取某个企业对应某个模块的全部api设置信息
     *
     * @param $eid
     * @param $mid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAll($eid, $mid) {
        $cacheKey = self::_k($eid, $mid);
        $apis = Cache::get($cacheKey);
        if (empty($apis)) {
            $apis = self::_q()->where(['enterprise_id' => $eid, 'module_id' => $mid])->select();
            $apis = remap($apis, "api");

            $moduleApis = SiteModuleApi::getApis($mid);
            $moduleApis = remap($moduleApis, "api");
            foreach ($moduleApis as $api => $moduleApi) {
                if (!isset($apis[$api])) {
                    $apis[$api] = [
                        'api' => $api,
                        'status' => 'allow',
                        'grant_fields' => $moduleApi['grant_fields']
                    ];
                }
            }

            Cache::set($cacheKey, $apis, 86400);
        }
        return $apis;
    }

    /**
     * 删除
     *
     * @param $mid
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function deleteByMid($mid) {
        return self::_q()->where(['module_id' => $mid])->delete();
    }

    /**
     * @return \think\db\Query
     */
    private static function _q() {
        return Db::name("EnterpriseModuleApi");
    }
}